--[[
	www.fh-wow.com/firehack/API.lua
]]

--
-- TABLES
--
-- Complete Object List table
ccR_tbl_GUID_Object_List = {}
-- Object list seperated into tables by Type
ccR_tbl_Object = {}
ccR_tbl_Item = {}
ccR_tbl_Container = {}
ccR_tbl_Unit = {}
ccR_tbl_Player = {}
ccR_tbl_GameObject = {}
ccR_tbl_DynamicObject = {}
ccR_tbl_Corpse = {}
--
-- Useful tables to make life easier
--
ccR_tbl_Attackable_Enemies = {}
ccR_tbl_Non_Attackable_Units = {}
ccR_tbl_Enemies_Around_Player_Within_44 = {}
ccR_tbl_Enemies_Around_Player_Within_16 = {}
ccR_tbl_Enemies_Around_Player_Within_12 = {}
ccR_tbl_Enemies_Around_Player_Within_10 = {}
ccR_tbl_Enemies_Around_Player_Within_8 = {}
ccR_tbl_Enemies_Around_Target_Within_16 = {}
ccR_tbl_Enemies_Around_Target_Within_12 = {}
ccR_tbl_Enemies_Around_Target_Within_10 = {}
ccR_tbl_Enemies_Around_Target_Within_8 = {}
--
-- Variable's
--
local vPlayerGUID = UnitGUID("Player")

function ccR_Gather_GUID_ObjectList()
	local vSize_Of_ccR_Table = # ccR_tbl_GUID_ObjectList							-- See how many objects ccR has in it's table
	local vSize_Of_Object_List = GetTotalObjects()									-- See how many objects FireHack has
	local vTimeToRefresh = ccRS.Object_List_Refresh									-- User's setting for how frequently to refresh the table
	if vTimeToRefresh == nil then vTimeToRefresh = 0.25 end							-- No user setting so use a default of 0.25s to refresh the table populating
	if vSize_Of_ccR_Table ~= vSize_Of_Object_List									-- If the amount of objects is different than what we have stored
	or (
		ccR_vLast_Gather_GUID_ObjectList_Time == nil
		or
		(GetTime()-ccR_vLast_Gather_GUID_ObjectList_Time) > vTimeToRefresh
	   )
	then
		-- Clear all the tables to repopulate them with new GUID's
		table.empty(ccR_tbl_GUID_Object_List)												
		table.empty(ccR_tbl_Object)
		table.empty(ccR_tbl_Item)
		table.empty(ccR_tbl_Container)
		table.empty(ccR_tbl_Unit)
		table.empty(ccR_tbl_Player)
		table.empty(ccR_tbl_GameObject)
		table.empty(ccR_tbl_DynamicObject)
		table.empty(ccR_tbl_Corpse)
		table.empty(ccR_tbl_Attackable_Enemies)
		table.empty(ccR_tbl_Non_Attackable_Units)
		table.empty(ccR_tbl_Enemies_Around_Player_Within_44)
		table.empty(ccR_tbl_Enemies_Around_Player_Within_16)
		table.empty(ccR_tbl_Enemies_Around_Player_Within_12)
		table.empty(ccR_tbl_Enemies_Around_Player_Within_10)
		table.empty(ccR_tbl_Enemies_Around_Target_Within_16)
		table.empty(ccR_tbl_Enemies_Around_Target_Within_12)
		table.empty(ccR_tbl_Enemies_Around_Target_Within_10)
		table.empty(ccR_tbl_Enemies_Around_Target_Within_8)

		local i
		for i = 1, GetTotalObjects() do															-- Grab the entire object list and put it into a table
			local vObjectGUID = IGetObjectListEntry(i)											-- Collect line i to save and determine it's Type
			tinsert(ccR_tbl_GUID_Object_List, vObjectGUID)										-- Store every single line into this table
			local vGUID_Type = IGetType(vObjectGUID)												-- Here we determine just what Type the GUID is
			if vGUID_Type == 0 then table.insert(ccR_tbl_Object, vObjectGUID)					-- Now we fill the appropiate tables with what Type the GUID is
			elseif IGetType(vObjectGUID) == 1 then table.insert(ccR_tbl_Item, vObjectGUID)		-- Items
			elseif IGetType(vObjectGUID) == 2 then table.insert(ccR_tbl_Container, vObjectGUID)	-- Containers
			elseif IGetType(vObjectGUID) == 3 then table.insert(ccR_tbl_Unit, vObjectGUID)		-- Units
			elseif IGetType(vObjectGUID) == 4 then table.insert(ccR_tbl_Player, vObjectGUID)		-- Player
			elseif IGetType(vObjectGUID) == 5 then table.insert(ccR_tbl_GameObject, vObjectGUID)	-- Game Objects
			elseif IGetType(vObjectGUID) == 6 then table.insert(ccR_tbl_DynamicObject, vObjectGUID) -- Dynamic Objects
			elseif IGetType(vObjectGUID) == 7 then table.insert(ccR_tbl_Corpse, vObjectGUID)		-- Corpses
			end

			-- AutoSkinning/AutoLooting
			if ccRS.UseAutoLoot or ccRS.USeAutoSkinning then
				if vGUID_Type == 3
				and fhGetDistance(vObjectGUID,vPlayerGUID) <= max(5, (13 / 6) + 15)
				and (
						(ccRS.UseAutoLoot and IIsLootable(vObjectGUID))
						or
						(ccRS.USeAutoSkinning and IIsSkinnable(vObjectGUID))
					)
				then
					IObjectInteract(vObjectGUID)
				end
			end

			-- Collect Enemies and Friendlies
			if (vGUID_Type == 3 or vGUID_Type == 4)		-- 3-4 for NPC/Players
			and IExists(vObjectGUID)					-- Does it still exist?
			then
				-- Attackable units
				if ICanAttack(vPlayerGUID, vObjectGUID) then
					-- Save this to the table
					tinsert(ccR_tbl_Attackable_Units, vObjectGUID)
					-- Find the distance from the attackable unit to the player
					local vAttackableUnitDistance = fhGetDistance(vObjectGUID, vPlayerGUID)
					-- Within 44 yards, This is mostly used for Auto Targetting
					if vAttackableUnitDistance <= 44 then
						tinsert(ccR_tbl_Enemies_Around_Player_Within_44, vObjectGUID)
					end
					-- Enemies within 16 yards of the player
					if vAttackableUnitDistance <= 16 then
						tinsert(ccR_tbl_Enemies_Around_Player_Within_16, vObjectGUID)
					end
					-- Enemies within 12 yards of the player
					if vAttackableUnitDistance <= 12 then
						tinsert(ccR_tbl_Enemies_Around_Player_Within_12, vObjectGUID)
					end
					-- Enemies within 10 yards of the player
					if vAttackableUnitDistance <= 10 then
						tinsert(ccR_tbl_Enemies_Around_Player_Within_10, vObjectGUID)
					end
					-- Enemies within 8 yards of the player
					if vAttackableUnitDistance <= 10 then
						tinsert(ccR_tbl_Enemies_Around_Player_Within_8, vObjectGUID)
					end
					-- Gather how many enemies are around the Target if we have one
					if UnitExists("target") == 1 then
						local vTargetGUID = UnitGUID("target")
						local vAttackableUnitDistance = fhGetDistance(vObjectGUID, vTargetGUID)
						-- Enemies within 16 yards of the target
						if vAttackableUnitDistance <= 16 then
							tinsert(ccR_tbl_Enemies_Around_Target_Within_16, vObjectGUID)
						end
						-- Enemies within 12 yards of the target
						if vAttackableUnitDistance <= 12 then
							tinsert(ccR_tbl_Enemies_Around_Target_Within_12, vObjectGUID)
						end
						-- Enemies within 10 yards of the target
						if vAttackableUnitDistance <= 10 then
							tinsert(ccR_tbl_Enemies_Around_Target_Within_10, vObjectGUID)
						end
						-- Enemies within 8 yards of the target
						if vAttackableUnitDistance <= 8 then
							tinsert(ccR_tbl_Enemies_Around_Target_Within_8, vObjectGUID)
						end
					end
				end
				-- Non-Attackable units
				if not ICanAttack(vPlayerGUID, vObjectGUID) then
					tinsert(ccR_tbl_Non_Attackable_Units, vObjectGUID)
				end
			end
		end
		ccR_vLast_Gather_GUID_ObjectList_Time = GetTime()	-- Save the time we filled the table
	end
end